<template>
  <div class="search-result">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad()"
    >
      <van-cell
        v-for="(item, index) in list"
        :key="index"
        :title="item.title"
        @click="enterArticle(item)"
      />
    </van-list>
  </div>
</template>
<script>
import { getSearchResult } from "@/api/search"
export default {
  props: {
    searchText: {
      type: String,
      required: true
    }
  },
  data: () => ({
    list: [],
    loading: false,
    finished: false,
    page: 1,
    perPage: 10
  }),
  methods: {
    async onLoad() {
      const { data } = await getSearchResult({
        page: this.page,
        per_page: this.perPage,
        q: this.searchText
      })
      const { results } = data.data
      this.list.push(...results)
      console.log(this.list)
      this.loading = false
      if (results.length) {
        this.page++
      } else {
        this.finished = true
      }
    },
    enterArticle(item) {
      this.$router.push({
        name: 'article',
        params: {
          articleId: item.art_id.toString()
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
</style>